package com.zxp.dubbo.consumer;

import com.zxp.client.User;
import com.zxp.client.UserService;
import lombok.SneakyThrows;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author zxp
 * @date 2021-08-24 10:10
 */
public class DubboConsummer {
    @SneakyThrows
    public static void main(String[] args) {

        ApplicationConfig applicationConfig = new ApplicationConfig("zxp-dubbo-consummer");

        //RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333?unicast=false");
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://192.168.56.160:2181");

        //
        ReferenceConfig referenceConfig = new ReferenceConfig();
        referenceConfig.setInterface(UserService.class);
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        //referenceConfig.setUrl("dubbo://172.17.219.209:20880/com.zxp.client.UserService"); // 直连方式

        referenceConfig.setLoadbalance(RandomLoadBalance.NAME); // 负载匀衡，随机

        UserService userService = (UserService) referenceConfig.get();
        User user = userService.getUser(1);
        System.out.println(user);

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {

            String line = reader.readLine();
            if (line.equals("quit")) {
                break;
            }

            System.out.println(userService.getUser(1));
        }
    }
}
